package com.zzx.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.zzx.model.Books;

@Controller
@RequestMapping("books")
@SessionAttributes(names= {"books"},types= {ArrayList.class})
public class BooksController {

	private Logger logger=LoggerFactory.getLogger(BooksController.class);
	
	private static List<Books> books=new ArrayList<>();
	
	static{
		books.add(new Books("神雕侠侣",89.7,"渣渣"));
		books.add(new Books("绝代双骄",189.7,"古龙"));
		books.add(new Books("大唐双龙传",89.7,"黄易"));
	}
	
	@RequestMapping("all")
	public String findAll(Map<String, Object> map) {
		//debug
		//info
		//warning
		//error
		logger.info("这就是我写出的日志！");
		
		map.put("books", books);
		return "bs/books";
	}
	
	@RequestMapping("del")
	public String del(@RequestParam("name") String name) {
		books.remove(new Books(name,0,null));
		return "redirect:all";//response.sendRedirect()
	}
	
	@RequestMapping("add")
	public String add() {
		return "add";
	}
	
	@RequestMapping("doadd")
	public String doadd(Books book) {
		System.out.println(book.getName()+book.getPrice()+book.getAuth());
		books.add(book);
		return "redirect:all";
	}
	
	@RequestMapping("update")
	public String update() {
		return "update";
	}
	
	@RequestMapping("doupdate")
	public String doupdate(@RequestParam("index") String name,Books book) {
		int indexOf = books.indexOf(new Books(name,38,"狗哥是真狗啊"));
		if(indexOf != -1) {
			books.set(indexOf, book);
		}
		System.out.println(book.getName()+book.getPrice()+book.getAuth());
		return "redirect:all";
	}
}
